Toggle Switch
Toggle Switch allows users to turn things on or off with an immediate effect.
#Examples
Think of ToggleSwitch
as a physical switch that allows the user to turn things on or off, like a light switch. Use ToggleSwitch
when the user has the freedom and control to change their settings as needed.
A ToggleSwitch
should always have a label and a default value, either true
(on) or false
(off).
const [checked, setChecked] = React.useState(false);
return <ToggleSwitch label="Label" value={checked} onChange={setChecked} />;
#Label Position
Depending on layout constraints, labels can be positioned to the left or right of a ToggleSwitch
. It is best to place the labels to the left of the ToggleSwitch
so that users can more easily read the text and understand the meaning in the language written from left to right.
const [checked, setChecked] = React.useState(false);
return (
<>
<ToggleSwitch
label="Label in the left"
labelPosition="left"
value={checked}
onChange={setChecked}
/>
<br />
<ToggleSwitch
label="Label in the right"
labelPosition="right"
value={checked}
onChange={setChecked}
/>
</>
);
#Custom Label
Use a custom label to explain what happens when a ToggleSwitch
is on or off. Explain how turning a switch on or off affects a user's decision.
const [checked, setChecked] = React.useState(false);
return (
<ToggleSwitch
label={
<InlineText emphasis={checked ? "medium" : "normal"}>
<Tooltip variant={{ type: "text" }} content="This is a tooltip">
Label with tooltip
</Tooltip>
</InlineText>
}
value={checked}
onChange={setChecked}
/>
);
Property | Description | Defined | Value |
---|---|---|---|
onChangeRequired | function Callback to switch the value | ||
valueRequired | boolean Determines the state of the toggle switch | ||
labelOptional | element Text to be displayed next to the toggle switch | ||
aria-labelOptional | string Text to describe the function of toggle switch | ||
aria-describedbyOptional | string ID of an an element that describes what the toggle switch is for | ||
disabledOptional | boolean Disabled state | ||
labelPositionOptional | "left" | "right" Should the label be displayed on the right or the left side of the toggle switch - default is right | ||
fullWidthOptional | boolean Should the toggle element be full width? | ||
data-observe-keyOptional | string Unique string, used by external script e.g. for event tracking | ||
classNameOptional | string Custom className that's applied to the outermost element (only intended for special cases) | ||
styleOptional | object Style object to apply custom inline styles (only intended for special cases) | ||
tabIndexOptional | number Tab index of the outermost HTML element of the component | ||
onKeyDownOptional | function Callback for onKeyDown event | ||
onMouseDownOptional | function Callback for onMouseDown event | ||
onMouseEnterOptional | function Callback for onMouseEnter event | ||
onMouseLeaveOptional | function Callback for onMouseLeave event | ||
onFocusOptional | function Callback for onFocus event | ||
onBlurOptional | function Callback for onBlur event |
#Guidelines
#Best practices
#Do not use when
#Accessibility
Explore detailed guidelines for this component: Accessibility Specifications